package it.valeandre.omino.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Locale;

import it.valeandre.omino.R;
import it.valeandre.omino.model.Match;
import it.valeandre.omino.ui.FormationView;
import it.valeandre.omino.ui.TimelineView;
import it.valeandre.omino.utils.Maps;
import it.valeandre.omino.utils.ResBundle;
import it.valeandre.omino.utils.Utils;

public class MatchFormationPageFragment extends BaseFragment {

    public static final int HOME_FORMATION = 0;
    public static final int AWAY_FORMATION = 1;

    private int mWitch;

    public static Fragment newInstance(Bundle matchData, int witch)
    {
        MatchFormationPageFragment myFragment = new MatchFormationPageFragment();
        myFragment.setWitch(witch);
        myFragment.setArguments(matchData);
        return myFragment;
    }

    public static Fragment newInstance(Bundle matchData, int witch, OnFragmentTouchListener listener)
    {
        MatchFormationPageFragment myFragment = new MatchFormationPageFragment();
        myFragment.setWitch(witch);
        myFragment.setArguments(matchData);
        myFragment.setOnFragmentTouchListener(listener);
        return myFragment;
    }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_match_detail_formation, container, false);
        Bundle matchData = getArguments();
        Match match  = (Match)matchData.getSerializable(ResBundle.KEY_BUNDLE_MATCH_DETAIL_DATA);
        Data data = new Data(match, getWitch());

        fillHeadData(rootView, data);

        FormationView formationView = (FormationView) rootView.findViewById(R.id.fragment_match_detail_formation_formation_view);
        formationView.fill(data);
        return rootView;
	}

    private void fillHeadData(ViewGroup rootView, Data data) {

        ImageView teamLogo = (ImageView) rootView.findViewById(R.id.fragment_match_detail_formation_team_logo);
        Utils.setLogoByName(data.teamName, teamLogo, Maps.LOGOS_DIMENSION_MID_ID);

        TextView teamName = (TextView) rootView.findViewById(R.id.fragment_match_detail_formation_team_name);
        teamName.setText(data.teamName);

        TextView teamModule = (TextView) rootView.findViewById(R.id.fragment_match_detail_formation_team_module);
        teamModule.setText(data.module);
    }

    private int getWitch() {
        return mWitch;
    }

    private void setWitch(int mWitch) {
        this.mWitch = mWitch;
    }

    public class Data {
        public Data(Match match, int witch) {
            if(witch == HOME_FORMATION) {
                String hometeamString = match.getHometeam() == null || match.getHometeam().trim().equals("") ? match.getHometeamRealtime() : match.getHometeam();
                teamName = hometeamString;
                module = match.getHomeTeamFormation();
                lineupGoalkeeper = match.getHomeLineupGoalkeeper();
                lineupDefense = match.getHomeLineupDefense();
                lineupMidfield = match.getHomeLineupMidfield();
                lineupForward = match.getHomeLineupForward();
            } else {
                String visitorteamString = match.getAwayteam() == null || match.getAwayteam().trim().equals("") ? match.getAwayteamRealtime() : match.getAwayteam();
                teamName = visitorteamString;
                module = match.getAwayTeamFormation();
                lineupGoalkeeper = match.getAwayLineupGoalkeeper();
                lineupDefense = match.getAwayLineupDefense();
                lineupMidfield = match.getAwayLineupMidfield();
                lineupForward = match.getAwayLineupForward();
            }
        }

        public String teamName;
        public String module;
        public String lineupGoalkeeper;
        public String lineupDefense;
        public String lineupMidfield;
        public String lineupForward;

    }

}
